<?php
App::uses('AppController', 'Controller');

class EventsController extends AppController 
{
	public $uses = array('Events','Clients','Services','Branches');
	public $components = array('Paginator');

	public function index() {	
		$this->set("title_for_layout",'Turnos');
		$pagination_settings= array();
		
		if(isset($this->request->query['keyword'])){
			$keyword = $this->request->query['keyword'];
			$pagination_settings['conditions']= array('Clients.dni LIKE' => $keyword."%");
			
		}

		$pagination_settings['limit'] =30;
		$this->Paginator->settings =$pagination_settings;
    $events = $this->Paginator->paginate('Events');	   
	
    $this->set('data',$events);			
	}

	public function add(){

		if(!empty($this->data))
		{			

			//var_dump($this->data);
			$service = $this->Services->findById($this->data['Events']['service_id']);
			$time_to=$service['Services']['time'];
			$time_from=$this->data['Events']['time_from'];
			$aux= date('H:i:s', strtotime('+'.$time_to.' minutes', strtotime($time_from)));
			$this->request->data['Events']['time_to']= $aux;
			$this->Events->set($this->data);
				if($this->Events->save()){
					$this->redirect('/events');
					$this->Session->setFlash(__("Error", true), 'flash', array('class' => 'alert alert-error'));	
				}	
		}
		$services = $this->Services->find('list');
		$services_codes = $this->Services->find('list',array('fields' => array('Services.id', 'Services.service_code')));		
		$clients_codes = $this->Clients->find('list',array('fields' => array('Clients.id', 'Clients.dni')));		
		$clients = $this->Clients->find('list');
		$branches = $this->Branches->find('list');
		$this->set('services',$services);	
		$this->set('services_codes',$services_codes);	
		$this->set('clients_codes',$clients_codes);	
		$this->set('clients',$clients);	
		$this->set('branches',$branches);	
		$this->set('page',"Nuevo");

	}

	public function add_alt(){

		//var_dump($this->request->data);

		$this->request->data['Events']['date'] =date('d-m-Y', strtotime( $this->data['Events']['date'])); 
		$this->request->data['Events']['time_from'] =  date('H:i',  strtotime($this->data['Events']['time_from']));
		$this->request->data =$this->request->data;	
		

		if(!empty($this->data['Events']['service_id']))
		{			
			$service = $this->Services->findById($this->data['Events']['service_id']);
			$time_to=$service['Services']['time'];
			$time_from=$this->data['Events']['time_from'];
			$aux= date('H:i:s', strtotime('+'.$time_to.' minutes', strtotime($time_from)));
			$this->request->data['Events']['time_to']= $aux;
			$this->Events->set($this->data);
				if($this->Events->save()){
					$this->redirect('/');
					$this->Session->setFlash(__("Error", true), 'flash', array('class' => 'alert alert-error'));	
				}		
		}
		$services = $this->Services->find('list');
		$services_codes = $this->Services->find('list',array('fields' => array('Services.id', 'Services.service_code')));		
		$clients_codes = $this->Clients->find('list',array('fields' => array('Clients.id', 'Clients.dni')));		
		$clients = $this->Clients->find('list');
		$branches = $this->Branches->find('list');
		$this->set('services',$services);	
		$this->set('services_codes',$services_codes);	
		$this->set('clients_codes',$clients_codes);	
		$this->set('clients',$clients);	
		$this->set('branches',$branches);	
		$this->set('page',"Nuevo");


	}




	public function edit($id=0)
	{
		if (!empty($this->data))
		{     
		  $this->Events->id = $id;  
		  $service = $this->Services->findById($this->data['Events']['service_id']);
			$time_to=$service['Services']['time'];
			$time_from=$this->data['Events']['time_from'];
			$aux= date('H:i:s', strtotime('+'.$time_to.' minutes', strtotime($time_from)));
			$this->request->data['Events']['time_to']= $aux;          
		  if ($this->Events->save($this->data)) 
		  {                
		  	//$this->Session->setFlash('Evento Actualizado.', 'flash', array('class' => 'thrill'));                
		  	$this->redirect('/events');            
		  }        
		}        
		else 
		{            
			$row= $this->Events->findById($id);
			//var_dump($row);
			$new_date = ($row['Events']['date']!='')? date("d-m-Y", strtotime($row['Events']['date'])) : '';
			//var_dump($new_date);
			$row['Events']['date'] = $new_date;
			$this->request->data =$row;
    }

		$services = $this->Services->find('list');
		$services_codes = $this->Services->find('list',array('fields' => array('Services.id', 'Services.service_code')));		
		$clients_codes = $this->Clients->find('list',array('fields' => array('Clients.id', 'Clients.dni')));		
		$clients = $this->Clients->find('list');

		$branches = $this->Branches->find('list');
		$this->set('services',$services);	
		$this->set('services_codes',$services_codes);	
		$this->set('clients_codes',$clients_codes);	
		$this->set('clients',$clients);	
		$this->set('branches',$branches);	
		$this->set('page',"Editar");	
		$this->render('add');
	}

	public function delay($id=0,$aviso=0)
	{
		
		if($aviso==1){
			$event = $this->Events->read(null,$id); 	
			$event['Events']['status'] = 2;
			$this->Events->save($event);
			$this->redirect("/events/add");

		}
		if (!empty($this->data))
		{     
		  $this->Events->id = $id;    
		  $service = $this->Services->findById($this->data['Events']['service_id']);
		  $date = date('Y-m-d', strtotime( $this->data['Events']['date_changed']));
		  $this->request->data['Events']['date_changed']=$date;

			$time_to=$service['Services']['time'];
			$time_from=$this->data['Events']['time_from_changed'];
			$aux= date('H:i:s', strtotime('+'.$time_to.' minutes', strtotime($time_from)));
			$this->request->data['Events']['time_to_changed']= $aux;        
		  if ($this->Events->save($this->data)) 
		  {                
		  	//$this->Session->setFlash('Evento Actualizado.', 'flash', array('class' => 'thrill'));                
		  	$this->redirect('/events');            
		  }        
		}        
		else 
		{            
			$row= $this->Events->findById($id);
			//var_dump($row);
			$new_date = ($row['Events']['date']!='')? date("d-m-Y", strtotime($row['Events']['date'])) : '';
			//var_dump($new_date);
			$row['Events']['date'] = $new_date;
			$this->request->data =$row;
    }

		$services = $this->Services->find('list');
		$services_codes = $this->Services->find('list',array('fields' => array('Services.id', 'Services.service_code')));		
		$clients_codes = $this->Clients->find('list',array('fields' => array('Clients.id', 'Clients.dni')));		
		$clients = $this->Clients->find('list');

		$branches = $this->Branches->find('list');
		$this->set('services',$services);	
		$this->set('services_codes',$services_codes);	
		$this->set('clients_codes',$clients_codes);	
		$this->set('clients',$clients);	
		$this->set('branches',$branches);	
		$this->set('page',"Posponer");	
		
	}


	public function get_all() {	

		App::uses('CakeTime', 'Utility');
		$this->layout=false;
		$this->render(false);	
		$branch_id= ( isset($this->data['branch_id']) )? (int) $this->data['branch_id'] : 0;

		if($branch_id==0){
			$events = $this->Events->find('all');
		}else{
			$events = $this->Events->find('all',array('conditions'=>array('Branches.id = '=> $branch_id)));
		}

    $event_listing =array() ; 
   	foreach ($events as $item) {
   		
   		if($item['Events']['is_changed']){

	   		$date= $item['Events']['date_changed'];
	   		$time_from= $item['Events']['time_from_changed'];
	   		$time_to= $item['Events']['time_to_changed'];
	   		$date_start= $date." ".$time_from;
	   		$date_end= $date." ".$time_to;
   		
   		}else{

	   		$date= $item['Events']['date'];
	   		$time_from= $item['Events']['time_from'];
	   		$time_to= $item['Events']['time_to'];
	   		$date_start= $date." ".$time_from;
	   		$date_end= $date." ".$time_to;
   			
   		}
   		$title='';
   		
   		$paid=(float)$item['Services']['price']-(float) $item['Events']['paid'];

   		$event_listing[]=array(
   			'id'=>$item['Events']['id'],
   			'title'=> $item['Services']['name']." - ". $item['Clients']['first_name'],//$title,
   			'start' =>$date_start,
   			'end' =>$date_end,
   			'allDay'=> false,
   			'paid'=>($paid==0)?true:false,
   			'description' =>'test.com',
   			'editable'=> true,
   			'backgroundColor'=> '#'.$item['Services']['color_code']	
   			);
   	}
 		
   	echo json_encode($event_listing);
	}

	public function get_by_date($branch_id=0, $date='')//$service_id=0,$date='')
	{
		$this->layout=false;
		$this->render(false);
		App::uses('CakeTime', 'Utility');		
		$new_date= CakeTime::format($date, '%Y-%m-%d');		
		$events = $this->Events->find(	'all',
										array(	
											'conditions'=>array('Events.branch_id'=>$branch_id,'Events.date'=>$new_date),
											  	'order' => array('Events.time_from DESC')));
		$event_listing=array();
		foreach ($events as $item) {
			$date= $item['Events']['date'];
	   		$time_from= $item['Events']['time_from'];
	   		$time_to= $item['Events']['time_to'];
	   		$date_start= $date." ".$time_from;
	   		$date_end= $date." ".$time_to;

			$event_listing[]=array(
	   			'id' =>$item['Events']['id'],
	   			'title' => $item['Services']['name']." - ".$item['Clients']['last_name']." ". $item['Clients']['first_name'],
	   			'start' =>$date_start,
	   			'end'  => $date_end,
	   			'allDay'=> false,
	   			//'url' =>'test.com',
	   			'editable'=> true,
	   			'backgroundColor'=> '#'.$item['Services']['color_code']	
	   			);
		}
		//var_dump($event_listing);
		echo json_encode($event_listing);
	}

	public function add_minute($event_id)
	{
		$a_time='00:12:30';
		$ten_minutes='10';
		$new_time = date('H:i:s', strtotime('+'.$ten_minutes.' minutes', strtotime($a_time)));
	}

	function filter(){
		$this->layout=false;	
		$pagination_settings= array();
		if(isset($this->request->data['keyword'])){
			$keyword = $this->request->data['keyword'];
			//$pagination_settings['conditions']= array('Clients.dni LIKE' => $keyword."%");	
			$conditions = array(				
						'OR'=>array(							
							'Clients.dni LIKE' => "%".$keyword."%",
							'Clients.last_name LIKE' => "%".ucfirst($keyword)."%",
							'Clients.first_name LIKE' => "%".ucfirst($keyword)."%")
						
				);
			//$pagination_settings['conditions']= array('Clients.last_name LIKE' => "%".ucfirst($keyword)."%");			
			$pagination_settings['conditions']=$conditions;// array('Clients.last_name LIKE' => "%".ucfirst($keyword)."%");			
		}
		$pagination_settings['limit'] =30;
		$this->Paginator->settings =$pagination_settings;
    $data = $this->Paginator->paginate('Events');	
    $this->set('data',$data);		
    $this->render('grid','ajax');   	
	}


	public function check_time(){
		$this->layout=false;		
		$this->render(false); 
		$result = json_encode(true);
		$today =  date('Y-m-d');
		$now = date("H:m:s");
		var_dump($today);
		var_dump($now);
		$events = $this->Events->find('all', array(
		    'conditions' => array(
		        'date >=' => $start_date,
		        'created <=' => $end_date . ' 23:59:59'
		)));

		echo $result;
	}



}